package com.sitech.openapi.controller;

import com.sitech.openapi.componet.SetHttpHeaderCmpt;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "baseController",description = "基类控制层")
@RestController
@Slf4j
public class AbstractController {

    @Autowired
    public SetHttpHeaderCmpt setHttpHeaderCmpt;

    @ExceptionHandler
    public ResponseEntity<Map<String, String>> ContactTaskError(Exception e, NativeWebRequest request) {

        Map<String, String> errors = new HashMap<>();
        errors.put("code", HttpStatus.BAD_REQUEST.toString());
        errors.put("reason", e.getMessage());
        errors.put("message",e.toString());
        errors.put("referenceError","");
        e.printStackTrace();

        HttpHeaders header = setHttpHeaderCmpt.setErrorHttpHeaders(request);
        ResponseEntity<Map<String, String>> re = new ResponseEntity<>(errors, header, HttpStatus.BAD_REQUEST);
        return re;
    }

    /**
     * 设置公共的消息头
     * @param headers 入参的消息头，主要是 X-CTG-Request-ID
     * @return 封装好的公共的
     */
    public HttpHeaders initOKHeader(HttpHeaders headers){
        return setHttpHeaderCmpt.setOKHttpHeaders(headers);
    }
}
